﻿#pragma once
#include"HashTable.h"

namespace egoist 
{
	//这样可以自己实现仿函数控制first的比较逻辑
	template<class K,class V, class Hash = HashFunc<K>>
	class unordered_map
	{
		struct MapkeyOfT
		{
			const K& operator()(const pair<K, V>& kv)
			{
				return kv.first;
			}
		};
	public:
		typedef typename egoist::HashTable<K, pair<const K, V>, MapkeyOfT,Hash>::Iterator iterator;
		typedef typename egoist::HashTable<K, pair<const K, V>, MapkeyOfT,Hash>::ConstIterator const_iterator;

		iterator begin()
		{
			return _ht.Begin();
		}

		iterator end()
		{
			return _ht.End();
		}

		const_iterator begin() const
		{
			return _ht.Begin();
		}

		const_iterator end() const
		{
			return _ht.End();
		}

		iterator find(const K& key)
		{
			return _ht.Find(key);
		}

		pair<iterator, bool> insert(const pair<K, V>& kv)
		{
			return _ht.Insert(kv);
		}

		V& operator[](const K& key)
		{
			pair<iterator, bool> ret = insert({ key,V() });
			return ret.first->second;
		}

	private:
		egoist::HashTable<K, pair<const K, V>, MapkeyOfT,Hash> _ht;
	};

	void Print(const unordered_map<string, string>& s)
	{
		unordered_map<string, string>::const_iterator it = s.begin();
		while (it != s.end())
		{
			//*it = 1;
			cout << it->first << ":" << it->second << endl;
			++it;
		}
	}

	void test_unordered_map()
	{
		//unordered_map<string, string> us;
		//us.insert({ "left", "左边" });
		//us.insert({ "sort","算法" });
		//unordered_map<string, string>::iterator it = us.begin();
		//while (it != us.end())
		//{
		//	//it->first = 1;
		//	cout << it->first << ":" << it->second << endl;
		//	++it;
		//}
		//cout << endl;
		//Print(us);

		unordered_map<string, string> dict;
		dict.insert({ "string", "字符串" });
		dict.insert({ "left", "" });

		dict["sort"];

		dict["left"] = "左边+剩余";
		dict["right"] = "右边";

		unordered_map<string, string>::iterator it = dict.begin();
		while (it != dict.end())
		{
			//it->first += 'x';
			//it->second += 'x';

			cout << it->first << ":" << it->second << endl;
			++it;
		}
		cout << endl;
	}
}